package com.vsoft.sms_cm;

import java.io.DataOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.core.db.DBGadgets;
import com.vsoft.framework.core.db.DBTrans;
import com.vsoft.framework.core.db.OperAgent;
import com.vsoft.framework.core.db.SQLArgs;
import com.vsoft.framework.core.esb.ServiceBus;
import com.vsoft.framework.core.util.BOHelper;
import com.vsoft.framework.core.util.LogCollect;
import com.vsoft.framework.core.util.XMLUtil;
import com.vsoft.framework.server.servlet.SessionData;
import com.vsoft.framework.server.servlet.HttpUtil;
import com.vsoft.anti_fra.*;

public class TestUpdateLog {

	private static Logger vLogger 	= 	Logger.getLogger(TestUpdateLog.class);
	private static String vName 	= 	TestUpdateLog.class.getName();
	private SessionData vSD 		= 	null;
	private String vDebugHome 		= 	"";
	private BusiObj		vInBO		=  new BusiObj();
	
	public String genAreaCode(BusiObj bo)throws BaseException
	{
		BusiObj retBO						=	new BusiObj();
		String listName						=	"RESULT";
		String sql							=	"select * from area where area_no=:area_no ";
		String order_num					=	bo.getString("order_num");
		String area_no						=	bo.getString("area_no");
		Connection	conn					=	bo.getConnection();
		
		String area_code					=	area_no+order_num;
		SQLArgs	vArg			= 	new SQLArgs();
		vArg.reset();
		//vArg.setArg("order_num", order_num);
		vArg.setArg("area_no", area_no);
		BusiObj rsBo 						= 	OperAgent.execQuery(conn, "Query order_info " ,sql, vArg);
	    vLogger.info("查询区域信息，返回数据是:\n" + BOHelper.boToXML(rsBo));
	    ArrayList<BusiObj> Al_area = rsBo.getBusiObjList(listName);
	    int size							=	Al_area.size();
	    if(size==0)//
	    {
	    	vLogger.info("根据区号"+area_no+"找不到区域编码,请联系系统管理员");
	    	area_code	=	"";
	    }
	    else if(size==1)
	    {
	    	area_code						=	Al_area.get(0).getString("cat_code");	//只有一条记录直接返回area_code
	    }
	    else//如果有多条，则不匹配的记录只能《=1
	    {
	    	String num_5					=	","+area_code.substring(0,5)+",";//取完整号码的前五位和前六位
	    	String num_6					=	","+area_code.substring(0,6)+",";
	    	int count						=	0;
	    	boolean flag					=	false;//匹配亳州的区号信息标志
	    	String area_code_match="",area_code_nomatch="";
	    	for(BusiObj obj:Al_area)
	    	{
	    		
	    		String cat_code 			=	obj.getString("cat_code");
	    		String bozhou 				=	obj.getString("bozhou");	
	    		if(bozhou.trim().equals(""))
	    		{
	    			area_code_nomatch		=	cat_code;
	    			count++;
	    			if (count>1)
	    				throw new BaseException("order_10002","根据区号"+area_no+"找到多条区域编码，无法定位区域编码,请联系系统管理员");
	    			flag					=	false;
	    			continue;
	    		}
	    		else
	    		{
	    			if(bozhou.trim().indexOf(num_5)>=0||bozhou.trim().indexOf(num_6)>=0)
	    			{
	    				flag				=	true;
	    				area_code_match		=	cat_code;
	    				vLogger.info("订购号码信息:\n order_num :" +order_num+"\n  area_no :"+ area_no+"\n 是bozhou号码。");
	    			}
	    			
	    		}
	    	}
	    	if(flag)
	    		area_code					=	area_code_match;
	    	else
	    		area_code					=	area_code_nomatch;
	    }
		vLogger.info("订购号码信息:\n order_num :" +order_num+"\n last area_no :"+ area_no+"\n area_code:"+area_code);
		
		retBO.setString("area_code", area_code);
		
		return area_code;
	}
	public static void main(String[] args)  {
	
		try{

			vLogger.info("---------------InitSystem-------------");
			Configuration.InitSystem();
			vLogger.info("---------------update data-------------");

			TestUpdateLog t 		= 	new TestUpdateLog();
			Connection conn 		= 	DBTrans.getConnection();
			String listName			=	"RESULT";
			String	tableName		=	args[0].trim().toUpperCase();
			String	colName			=	"";
			if(tableName.equals("LOG_BIZSU"))
			{
				colName				=	"LOG_BIZSU_SN";
			}
			else if(tableName.equals("LOG_BIZSF"))
			{
				colName				=	"LOG_BIZSF_SN";
			}
			else
			{
				vLogger.info("---------------无法更新-------------"+tableName+"数据！！！");
				return;
			}
			vLogger.info("---------------begin query to be updated data-------------");
			String 	sql				=	" select order_num order_num,area_no area_no, " +
										colName +" "+colName+
										" from  " +tableName;
			
			SQLArgs	vArg			= 	new SQLArgs();
			BusiObj rsBo 			= 	OperAgent.execQuery(conn, "Query order_info " ,sql, vArg);
			ArrayList<BusiObj> Al_log = rsBo.getBusiObjList(listName);
			String	logSn			=	"";
			BusiObj areaBo 			=	new BusiObj();
			String	area_code		=	"";
			String	updateSql		=	" update "+tableName+" set area_code=:area_code where  "+colName+"=:"+colName;
			int		cnt				=	0;
			vLogger.info("---------------begin update data-------------");
			for(BusiObj obj:Al_log)
			{
	    		String order_num 	=	obj.getString("order_num");
	    		String area_no 		=	obj.getString("area_no");	
	    		if(area_no==null||area_no.trim().equals(""))
	    			continue;
	    		areaBo.setConnection(conn);
	    		areaBo.setString("order_num", order_num);
	    		areaBo.setString("area_no", area_no);
	    		area_code			=	t.genAreaCode(areaBo);
	    		logSn				=	obj.getString(colName);
				vArg.reset();
				vArg.setArg("area_code", area_code);
				vArg.setArg(colName, logSn);
				OperAgent.execUpdate(conn, "update  "+tableName,updateSql, vArg);
				vLogger.info("=====================update "+tableName+"   sql:" + updateSql);
				cnt++;
	    		
			}
			vLogger.info("=====================update "+tableName+" cnt="+cnt);
			
			DBGadgets.check(conn);

			conn.rollback();
			//conn.commit();
			conn.close();
			//vLogger.info("返回数据是:\n\n\n\n" + BOHelper.boToXML(retBo));

			//////////////////////////////

		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		catch(BaseException e)
		{
			e.printStackTrace();
			vLogger.error(e.ErrMethod);
			vLogger.error(e.SysMsg);
		}
			vLogger.info("---------------Game Over-------------");
	}	
	
		

	private int  init()
	{
		int iRet = 0;
		vSD = new  SessionData();
		vSD.vRoleCode = new String[] {"1"};
		vSD.vStaffCode = "1";
		vSD.vStaffNo = "0001";
		vSD.vStaffName = "test";
		
	
		vDebugHome = System.getProperty("DEBUG_HOME");
		if (vDebugHome == null)
		{
			iRet = -1;
			vLogger.error("需要设置DEBUG_HOME环境变量来指定debug 文件所在的目录");
		}
		return iRet;
		
	}
	
}
